#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
A module for finding scattering directions of a max on a givven density grid.
'''
import sys
from optparse import OptionParser
from plane import Plane
from grid_plane import PlaneGrid
import fr_parser
#from scitools.all import *

def return_plot(grid, plane):
    #mplane = Plane(int(m[0]), int(m[1])).normal() # plane of maximum
    plane = Plane(int(plane.dir), int(plane.dip))
    plane_value = grid.get(plane)
    # build a set of profiles starting in mplane and oritnted in different 
    # dirrections.
    profiles = {} # dictionary of profiles and directions
    min_value = 0 # plane_value * .3 # minimum value in cell
    for dir in range (1,360,05):
        a = (plane.dir - dir) % 360 
        if (a>90 and a<180) or (a<360 and a>270): dir = (dir+180)%360
        dplane = plane.get_perpendicular_between(Plane(dir, .0001)).normal()
        angle_val = dplane.return_angle_between(plane)
        angles_sub = dplane.dir-plane.dir
        points = [] # list of points in a profile
        if (angles_sub>0 and angles_sub<180) or\
           (angles_sub<-180 and angles_sub>-360):
            for angle in range(angle_val, angle_val+90, 1):
                a = dplane.rotated(-1*angle)
                if a.dip > 0: a = Plane(a.dir+180, a.dip*-1)
                # get a value from a grid
                val = grid.get(plane=Plane(int(a.dir),int(a.dip))) 
                if len(points)>2 and (val < min_value or val > points[-1][1]):
                    profiles[dir]=len(points)
                    break
                points.append([a, val])
        else:
            for angle in range(angle_val, angle_val+90, 1):
                a = dplane.rotated(angle)
                if a.dip > 0: a = Plane(a.dir+180, a.dip*-1)
                # get a value from a grid
                val = grid.get(plane=Plane(int(a.dir),int(a.dip))) 
                if len(points)>2 and (val < min_value or val > points[-1][1]):
                    profiles[dir]=len(points)
                    break
                points.append([a, val])
    profiles_length=[] # list of profiles length 
    profiles_length_old=[] # list of profiles length 
    profiles_dir=[] # list of profiles directions
    for i in profiles:
        profiles_dir.append(i)
    profiles_dir.sort()
    for i in profiles_dir:
        profiles_length_old.append(profiles[i])
    # bluring of the profile
    for i in range(len(profiles_length_old)):
        val_back = profiles_length_old[(i-1)% len(profiles_length_old)]
        val      = profiles_length_old[i]
        val_next = profiles_length_old[(i+1)% len(profiles_length_old)]
        profiles_length.append((val_back+val+val_next)/3.0)
    return [profiles_length, profiles_dir]
    #plot(   profiles_dir, profiles_length, \
    #        curent_scatering_dir, curent_scatering_length, \
    #        axis=(0, 360, min(profiles_length)-2, max(profiles_length)+2),\
    #        ylabel="Length of a profile", \
    #        xlabel="Direction of a profile")
